package com.quan.site.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.quan.commons.core.MyBaseServiceImpl;
import com.quan.commons.utils.PageUtils;
import com.quan.site.commons.vo.SitePageListVo;
import com.quan.site.entity.SiteNav;
import com.quan.site.entity.SitePageCate;
import com.quan.site.entity.SitePageList;
import com.quan.site.mapper.SitePageListMapper;
import com.quan.site.service.SiteNavService;
import com.quan.site.service.SitePageCateService;
import com.quan.site.service.SitePageListService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;


@Slf4j
@Service
public class SitePageListServiceImpl extends MyBaseServiceImpl<SitePageListMapper, SitePageList> implements SitePageListService {

    @Autowired
    private SitePageListMapper mapper;

    @Autowired
    private SiteNavService siteNavService;
    @Autowired
    private SitePageCateService cateService;

    @Override
    public SitePageListVo getInfo(Long id) {
        final SitePageList projectCenter = super.getById(id);

        SitePageListVo bean = new SitePageListVo();
        BeanUtils.copyProperties(projectCenter, bean);

        if (null != bean.getNavId()) {
            // 部门反推获取节点的父路径
            bean.setCascaderNavPathVals(this.siteNavService.findPath(bean.getNavId()));
        }

        return bean;
    }

    @Override
    public List<SitePageListVo> queryList(PageUtils pageUtils, SitePageListVo vo) {
        QueryWrapper<SitePageList> queryWrapper = new QueryWrapper<SitePageList>(vo);
        if (StringUtils.isNotBlank(vo.getPageTypes())) {
            queryWrapper.and(w -> {
                w.in("page_type", vo.getPageTypes().split(","));
            });
        }

        List<SitePageList> list = super.list(pageUtils, queryWrapper);
        List<SitePageListVo> collect = list.stream().map(item -> {
            SitePageListVo bean = new SitePageListVo();
            BeanUtils.copyProperties(item, bean);
            return bean;
        }).collect(Collectors.toList());

        return collect;
    }

    @Override
    public PageUtils queryPage(PageUtils pageUtils, SitePageListVo vo) {
        QueryWrapper<SitePageList> queryWrapper = new QueryWrapper<SitePageList>(vo);

        if (StringUtils.isNotBlank(vo.getKeyword())) {
            queryWrapper.and(w -> {
                w.like("title", vo.getKeyword());
            });
        }

        if (StringUtils.isNotBlank(vo.getPageTypes())) {
            queryWrapper.and(w -> {
                w.in("page_type", vo.getPageTypes().split(","));
            });
        }

        final IPage<SitePageList> page = super.selectPage(pageUtils, queryWrapper);

        PageUtils ps = new PageUtils(page);


        final List<SitePageList> records = page.getRecords();
        final List<SitePageList> collect = records.stream().map((item) -> {
            SitePageListVo bean = new SitePageListVo();
            BeanUtils.copyProperties(item, bean);

            // 获取导航名称
            if (null != item.getNavId()) {
                final SiteNav siteNav = this.siteNavService.getById(item.getNavId());
                if (null != siteNav) {
                    bean.setNavName(siteNav.getName());
                }
            }

            // 获取分类名称
            if (null != item.getCateId()) {
                final SitePageCate cate = this.cateService.getById(item.getCateId());
                if (null != cate) {
                    bean.setCateName(cate.getName());
                }
            }

            return bean;
        }).collect(Collectors.toList());

        ps.setRecords(collect);
        return ps;
    }

    @Override
    public void changeStatus(Long id, Integer status) {
        final LambdaUpdateWrapper<SitePageList> lambda = new UpdateWrapper<SitePageList>().lambda();
        lambda.eq(SitePageList::getId, id);
        lambda.set(SitePageList::getStatus, status);
        super.update(lambda);
    }

    @Override
    public void changeTitleBold(Long id, Integer titleBold) {
        final LambdaUpdateWrapper<SitePageList> lambda = new UpdateWrapper<SitePageList>().lambda();
        lambda.eq(SitePageList::getId, id);
        lambda.set(SitePageList::getTitleBold, titleBold);
        super.update(lambda);
    }

    @Override
    public void changePageTop(Long id, Integer pageTop) {
        final LambdaUpdateWrapper<SitePageList> lambda = new UpdateWrapper<SitePageList>().lambda();
        lambda.eq(SitePageList::getId, id);
        lambda.set(SitePageList::getPageTop, pageTop);
        super.update(lambda);
    }

    @Override
    public void changeShowIndex(Long id, Integer showIndex) {
        final LambdaUpdateWrapper<SitePageList> lambda = new UpdateWrapper<SitePageList>().lambda();
        lambda.eq(SitePageList::getId, id);
        lambda.set(SitePageList::getShowIndex, showIndex);
        super.update(lambda);
    }

    public void copyDataToMobile() {
        List<SitePageList> list = super.list(new LambdaQueryWrapper<SitePageList>());
        list.stream().map(item -> {
            SitePageList mobile = new SitePageList();
            BeanUtils.copyProperties(item, mobile);
            mobile.setId(null);
            mobile.setTerminal(2);
            return mobile;
        }).forEach(mobile -> {
            super.save(mobile);
        });
    }
}